// pages/staff/medical/create.js
const medicalService = require('../../../services/medical');
const appointmentService = require('../../../services/appointment');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    appointmentId: null,    // 预约ID
    petId: null,            // 宠物ID
    appointment: null,      // 预约信息
    diagnosis: '',          // 诊断结果
    treatment: '',          // 治疗方案
    remark: '',             // 备注
    isLoading: true,        // 是否正在加载
    isSubmitting: false,    // 是否正在提交
    error: null             // 错误信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('创建健康档案页面加载, 参数:', options);
    
    if (!options.appointmentId) {
      this.setData({
        isLoading: false,
        error: '缺少预约ID'
      });
      return;
    }
    
    this.setData({
      appointmentId: options.appointmentId,
      petId: options.petId || null
    });
    
    this.loadAppointment();
  },

  /**
   * 加载预约信息
   */
  loadAppointment: function () {
    this.setData({
      isLoading: true,
      error: null
    });
    
    appointmentService.getAppointmentDetail(this.data.appointmentId)
      .then(res => {
        console.log('获取预约详情成功:', res);
        
        if (!res || !res.id) {
          this.setData({
            isLoading: false,
            error: '获取预约详情失败'
          });
          return;
        }
        
        this.setData({
          appointment: res,
          petId: res.petId || this.data.petId,
          isLoading: false
        });
        
        // 检查是否已存在健康档案
        if (res.medicalRecordId) {
          // 跳转到健康档案详情页
          wx.redirectTo({
            url: `/pages/staff/medical/detail?id=${res.medicalRecordId}`
          });
        }
      })
      .catch(err => {
        console.error('获取预约详情失败:', err);
        this.setData({
          isLoading: false,
          error: err.message || '获取预约详情失败'
        });
      });
  },

  /**
   * 诊断结果输入事件
   */
  inputDiagnosis: function (e) {
    this.setData({
      diagnosis: e.detail.value
    });
  },

  /**
   * 治疗方案输入事件
   */
  inputTreatment: function (e) {
    this.setData({
      treatment: e.detail.value
    });
  },

  /**
   * 备注输入事件
   */
  inputRemark: function (e) {
    this.setData({
      remark: e.detail.value
    });
  },

  /**
   * 提交健康档案
   */
  submitMedicalRecord: function () {
    // 表单验证
    if (!this.data.diagnosis.trim()) {
      wx.showToast({
        title: '请输入诊断结果',
        icon: 'none'
      });
      return;
    }
    
    if (!this.data.petId) {
      wx.showToast({
        title: '缺少宠物ID',
        icon: 'none'
      });
      return;
    }
    
    this.setData({
      isSubmitting: true
    });
    
    // 构建提交数据
    const data = {
      petId: this.data.petId,
      appointmentId: this.data.appointmentId,
      diagnosis: this.data.diagnosis,
      treatment: this.data.treatment || '',
      remark: this.data.remark || ''
    };
    
    console.log('提交健康档案数据:', data);
    
    medicalService.addMedicalRecord(data)
      .then(res => {
        console.log('创建健康档案成功:', res);
        
        this.setData({
          isSubmitting: false
        });
        
        wx.showToast({
          title: '创建成功',
          icon: 'success'
        });
        
        // 延迟返回上一页
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      })
      .catch(err => {
        console.error('创建健康档案失败:', err);
        
        this.setData({
          isSubmitting: false
        });
        
        wx.showToast({
          title: err.message || '创建失败',
          icon: 'none'
        });
      });
  },

  /**
   * 返回上一页
   */
  navigateBack: function () {
    wx.navigateBack();
  }
}); 